<!-- 文件上传 -->
<template>
  <Progress :percent="percent" ></Progress>
</template>
<script>
  export default {
    name:'UploadFile',
    props:{
      //允许文件个数默认为5
      fileObject: {
        type: Object,
        default: function(){
          return {};
        }
      },
      fileIndex:{
        type: [Number,String],
        required: true,
      },
      classify:{//上传到哪个类目
        type: [Number,String],
        default: 0
      }
    },
    data(){
      return{
        percent:0,//进度条
      }
    },
    created: function() {
      
    },
    mounted: function () {
      this.uploadImg();
    },
    computed: {
      
    },
    methods: {
      //图片上传
      uploadImg:function(){
        let file=this.fileObject.file;
        let putExtra = {
          fname: "",
          params: { },
          mimeType:  ["image/png", "image/jpeg", "image/gif"]
        },
        config = { },
        key,
        token,
        imgurl,
        that=this;//file =this.$refs.uploadfile.files[0],
        if(file){
          this.$http.get("/admin/qnytoken",{
            params: {
              "filename": file.name,
              "filesize":file.size,
              "classify":that.classify,
            }
          })
          .then(function (response) {
            key = response.data.filename;
            token=response.data.token;
            imgurl=response.data.imgurl;
            let observable = that.$qiniu.upload(file, key, token, putExtra, config);
            let subscription = observable.subscribe({
              next(response){
                that.percent=response.total.percent;
              },
              error(err){
                console.log(err);
              },
              complete(response){
                that.fileObject.isUpload=true;
                that.$emit("upsuc",that.fileIndex);
              }
            });
          });

        }
      },
    }
  }
</script>
<style >
  
</style>
